const db = require('../config/database');
const { v4: uuidv4 } = require('uuid');

class Log {
  // 获取所有日志
  static async getAll() {
    try {
      const [rows] = await db.query('SELECT * FROM lib_tool ORDER BY no');
      return rows;
    } catch (error) {
      throw error;
    }
  }

  // 根据 ID 获取日志
  static async getById(no) {
    try {
      const [rows] = await db.query('SELECT * FROM lib_tool WHERE no = ?', [no]);
      return rows[0];
    } catch (error) {
      throw error;
    }
  }

  // 创建新日志
  static async create({ name }) {
    const uuid = uuidv4();
    const [result] = await db.query('INSERT INTO lib_tool (uuid, name) VALUES (?, ?)', [uuid, name]);
    return { uuid, name };
  }

  // 更新日志
  static async update(no, logData) {
    try {
      const { uuid, name } = logData;
      const [result] = await db.query(
        'UPDATE lib_tool SET uuid = ?, name = ? WHERE no = ?',
        [uuid, name, no]
      );
      return result.affectedRows > 0;
    } catch (error) {
      throw error;
    }
  }

  // 删除日志
  static async delete(no) {
    try {
      const [result] = await db.query('DELETE FROM lib_tool WHERE no = ?', [no]);
      return result.affectedRows > 0;
    } catch (error) {
      throw error;
    }
  }
}

module.exports = Log; 